<template>
	<view>
		<cu-custom bgColor="bg-blue" :isBack="true"><block slot="content">地址管理</block></cu-custom>
		<view class="padding">
			<view class="solid-bottom bg-white"  v-for="(item,index) in list">
				<view class="text-sm padding-sm text-grey">
					<view class="content fl text-ablack text-xl" style="margin-left: 15upx;">
						{{item.name}}
					</view>
					<view class="action fl text-df text-ablack margin-left-xs" style="margin-top: 15upx;">
						{{item.phone}}
					</view>
					<view class="action fr text-df" v-show="item.selected == 1">
						<button class="cu-btn round line-blue text-sm" role="button" style="height: 45rpx;padding: 0 15rpx;margin-top: 15upx;">已默认</button>
					</view>
					<view class="action fr text-df" v-show="item.selected == 0">
						<button class="cu-btn round bg-blue text-sm" role="button" style="height: 45rpx;padding: 0 15rpx;margin-top: 15upx;" @click="selectAddress(item.id)">默认地址</button>
					</view>
					<view class="action fr text-df">
						<button class="cu-btn round line-red text-sm" role="button" style="height: 45rpx;padding: 0 15rpx;margin-top: 15upx;margin-right: 15upx;" @click="deleteAddress(item.id)">删除</button>
					</view>
					<view class="action fr text-df">
						<button class="cu-btn round line-green text-sm" role="button" style="height: 45rpx;padding: 0 15rpx;margin-top: 15upx;margin-right: 15upx;"  @click="toPage('goodspoor','addressopera',item.id)">编辑</button>
					</view>
				</view>
				<view style="clear: both;"></view>
				<view class="text-sm padding-sm text-grey">
					<view class="content fl text-gray text-df">
						{{item.detail}}
					</view>
				</view>
				<view style="clear: both;"></view>
				<view style="width: 100%;height: 20upx;"></view>
			</view>
			<view style="width: 100%;height: 40upx;"></view>
			<view class="flex flex-direction" @click="toPage('goodspoor','addressopera')">
				<button class="cu-btn bg-green lg text-bold text-l"  style="line-height: 2.8em;width: 50%;margin-left: 25%;">新建地址</button>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				list:[],
				count:0,
			};
		},
		onLoad() {
		},
		onShow() {
			this.loadlist();
		},
		methods: {
			toPage(folder,name,id=-1){
				if(id==-1){
					uni.navigateTo({
						url: `/pages/${folder}/${name}`
					});
				}else{
					uni.navigateTo({
						url: `/pages/${folder}/${name}?id=${id}`
					});
				}
			},
			loadlist(){
				let _this = this;
				uni.request({
					url: _this.$globals.apibase+'/api/Luckorder/addressList',
					data: {
						token: uni.getStorageSync('token'),
					},
					method: 'POST',
					header: {
						'content-type': 'application/json'
					},
					success: (res) => {
						if(res.data.code == '401'){
							uni.setStorageSync('token', '');
							_this.$uniApi.trylogin();
						}
						if(res.data.code == '1'){
							_this.count = res.data.data.count;
							_this.list = res.data.data.list;
						}
					}
				});
			},
			selectAddress(id){
				let _this = this;
				uni.showModal({
					title: '提示',
					content: '是否确认将该地址设为默认地址',
					success: function (res) {
						if (res.confirm) {
							uni.request({
								url: _this.$globals.apibase+'/api/Luckorder/selectAddress',
								data: {
									token: uni.getStorageSync('token'),
									id:id,
								},
								method: 'POST',
								header: {
									'content-type': 'application/json'
								},
								success: (res) => {
									if(res.data.code == '401'){
										uni.setStorageSync('token', '');
										_this.$uniApi.trylogin();
									}
									if(res.data.code == '1'){
										_this.loadlist();
										uni.showToast({
											icon: 'none',
											title: '设置默认地址成功',
											duration:5000
										});
									}
								}
							});
						} else if (res.cancel) {
						}
					}
				});
			},
			deleteAddress(id){
				let _this = this;
				uni.showModal({
					title: '提示',
					content: '是否确认删除该地址',
					success: function (res) {
						if (res.confirm) {
							uni.request({
								url: _this.$globals.apibase+'/api/Luckorder/deleteAddress',
								data: {
									token: uni.getStorageSync('token'),
									id:id,
								},
								method: 'POST',
								header: {
									'content-type': 'application/json'
								},
								success: (res) => {
									if(res.data.code == '401'){
										uni.setStorageSync('token', '');
										_this.$uniApi.trylogin();
									}
									if(res.data.code == '1'){
										_this.loadlist();
										uni.showToast({
											icon: 'none',
											title: '地址删除成功',
											duration:5000
										});
									}
								}
							});
						} else if (res.cancel) {
						}
					}
				});
			},
		}
	}
</script>

<style>
	.fl{
		float: left;
	}
	.fr{
		float: right;
	}
	.cu-form-group .title {
		min-width: calc(4em + 15px);
	}
</style>
